﻿using System;
using SlimDX;
namespace LibRadiant.Main
{
    public class Frame
    {
        private Vector3 mPosition = new Vector3();
        public Vector3 Position
        {
            get
            {
                return mPosition;
            }

            set
            {
                mDirtyFlag = true;
                mPosition = value;
            }
        }

        private Vector3 mRotation = new Vector3();
        public Vector3 Rotation
        {
            get
            {
                return mRotation;
            }

            set
            {
                mDirtyFlag = true;
                mRotation = value;
            }
        }

        private Vector3 mScale = new Vector3(1, 1, 1);
        public Vector3 Scale
        {
            get
            {
                return mScale;
            }

            set
            {
                mDirtyFlag = true;
                mScale = value;
            }
        }

        protected bool mDirtyFlag = true;
        Matrix mMatrix;
        public virtual Matrix Matrix
        {
            get
            {
                if (mDirtyFlag)
                {
                    mMatrix = CalculateMatrix(mPosition, mRotation, mScale);
                    mDirtyFlag = false;
                }
                return mMatrix;
            }

            //很不幸的，set有個很大的問題，就是他沒辦法直接算出Scale....
            //set{
            //}

        }

        //注意，Matrix是struct不是class
        static public Matrix CalculateMatrix(Vector3 inPosition, Vector3 inRotation, Vector3 inScale)
        {
            Matrix ret = new Matrix();
            ret = Matrix.Identity;
            ret *= Matrix.RotationYawPitchRoll(inRotation.Y, inRotation.X, inRotation.Z);
            ret *= Matrix.Scaling(inScale.X, inScale.Y, inScale.Z);
            ret *= Matrix.Translation(inPosition.X, inPosition.Y, inPosition.Z);
            return ret;
        }

    }
}
